package 不定长滑动窗口;

public class _1493删掉一个元素以后全为1的最长子数组 {
    public static int longestSubarray(int[] nums) {
         // 其实只需要统计0的数量
         int maxResult = 0;
         int left = 0;
         int zeroCount = 0;
 
         for (int right = 0; right < nums.length; right++) {
             if (nums[right] == 0) {
                 zeroCount++;
             }
 
             while (zeroCount > 1) {
                 if (nums[left] == 0) {
                     zeroCount--;
                 }
                 left++;
             }
 
             maxResult = Math.max(maxResult, right - left);
         }
         return maxResult;
    }

    public static void main(String[] args) {
        System.out.println("=="+longestSubarray(new int[]{1,1,0,1}));
        System.out.println("=="+longestSubarray(new int[]{0,1,1,1,0,1,1,0,1}));
        System.out.println("=="+longestSubarray(new int[]{1,1,1}));
    }
}
